<?php
 /*****************************************************************************
 * createInvoice.php                                                          *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * createInvoice.php is used as the driver file for creating a new invoice    *
 * record from the data passed by POST from newInvoice.php.                   *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      createInvoice.php                                               *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/


  // Require session.php for user access control
	require "include/session.php";

  // If user is not logged in, redirect to main.php
	if(!$session->logged_in){
		header("Location: main.php");
		exit();
  // If user is logged in...
	} else {
    // Require config.inc.php for configuration variables
		require_once "config.inc.php";

		// Get form data posted from newInvoice.php
		$customerID=$_POST['customerID'];

		$item1Qty=$_POST['item1Qty'];
		$item2Qty=$_POST['item2Qty'];
		$item3Qty=$_POST['item3Qty'];
		$item4Qty=$_POST['item4Qty'];
		$item5Qty=$_POST['item5Qty'];
		$item6Qty=$_POST['item6Qty'];
		$item7Qty=$_POST['item7Qty'];
		$item8Qty=$_POST['item8Qty'];
		$item9Qty=$_POST['item9Qty'];
		$item10Qty=$_POST['item10Qty'];

		$item1Name=$_POST['item1Name'];
		$item2Name=$_POST['item2Name'];
		$item3Name=$_POST['item3Name'];
		$item4Name=$_POST['item4Name'];
		$item5Name=$_POST['item5Name'];
		$item6Name=$_POST['item6Name'];
		$item7Name=$_POST['item7Name'];
		$item8Name=$_POST['item8Name'];
		$item9Name=$_POST['item9Name'];
		$item10Name=$_POST['item10Name'];

		$item1Rate=$_POST['item1Rate'];
		$item2Rate=$_POST['item2Rate'];
		$item3Rate=$_POST['item3Rate'];
		$item4Rate=$_POST['item4Rate'];
		$item5Rate=$_POST['item5Rate'];
		$item6Rate=$_POST['item6Rate'];
		$item7Rate=$_POST['item7Rate'];
		$item8Rate=$_POST['item8Rate'];
		$item9Rate=$_POST['item9Rate'];
		$item10Rate=$_POST['item10Rate'];

		$discount=$_POST['discount'];
		$taxRate=$_POST['taxRate'];
		$notes=$_POST['notes'];


		// GET SEED VALUE
		// Connect to database
		mysql_connect($path, $username, $password);
		// Select database or die with error message
		@mysql_select_db($database) or die("Unable to select database<br />" . mysql_error());

		// Define MySQL query for obtaining key value of last record
		$query = "SELECT * FROM tblInvoices ORDER BY 'key' DESC LIMIT 1";
		// Execute the MySQL query, if error die with error message
		mysql_query($query) or die(mysql_error());

		// Store returned row in $result
		$result = mysql_query($query);

		// Determine the number of rows returned
		$num = mysql_numrows($result);

		// DETERMINE IF ANY ROWS WERE RETURNED BY THE MySQL QUERY ON LINE 100
		// If there were rows returned...
		if($num != 0) {
      // $seed value is the last record's key value + 1
      // Example: Last record had key value of 3, $seed value would be (3+1)=4
			$seed = mysql_result($result,0,"key") + 1;
    // If no rows were returned, $seed value is 1
		} else {
			$seed = 1;
		}


		// DEFINE DATA VARIABLES
		// Obtain today's date
		$date = date("m-d-Y");
		// Obtain the current year
		$year = date("Y");

    // Calculate itemTotal for each row
		$item1Total = $item1Qty * $item1Rate;
		$item2Total = $item2Qty * $item2Rate;
		$item3Total = $item3Qty * $item3Rate;
		$item4Total = $item4Qty * $item4Rate;
		$item5Total = $item5Qty * $item5Rate;
		$item6Total = $item6Qty * $item6Rate;
		$item7Total = $item7Qty * $item7Rate;
		$item8Total = $item8Qty * $item8Rate;
		$item9Total = $item9Qty * $item9Rate;
		$item10Total = $item10Qty * $item10Rate;


		// Calculate the subTotal
		$subTotal = 0;
		$subTotal += $item1Total;
		$subTotal += $item2Total;
		$subTotal += $item3Total;
		$subTotal += $item4Total;
		$subTotal += $item5Total;
		$subTotal += $item6Total;
		$subTotal += $item7Total;
		$subTotal += $item8Total;
		$subTotal += $item9Total;
		$subTotal += $item10Total;

    // Calculate the discountedSubTotal
		$discountedSubTotal = $subTotal - $discount;

    // Convert tax rate percentage to decimal
		$taxRate = $taxRate / 100;
		// Calculate taxes
		$tax = $discountedSubTotal * $taxRate;

    // Calculate total
		$total = $discountedSubTotal + $tax;


		// Create value of $invoiceNum
		if($seed>0 && $seed<10) {
			$invoiceNum = "{$year}000{$seed}";
		} else {
			if($seed>=10 && $seed<100) {
				$invoiceNum = "{$year}00{$seed}";
			} else {
				if($seed>=100 && $seed<1000) {
					$invoiceNum = "{$year}0{$seed}";
				} else {
					$invoiceNum = "{$year}{$seed}";
				}
			}
		}


		// INSERT VALUES
		// Define insert MySQL query
		$insert="INSERT INTO `tblInvoices` VALUES ('', '$customerID', '$invoiceNum', '$date', '$item1Qty', '$item1Name', '$item1Rate', '$item1Total', '$item2Qty', '$item2Name', '$item2Rate', '$item2Total', '$item3Qty', '$item3Name', '$item3Rate', '$item3Total', '$item4Qty', '$item4Name', '$item4Rate', '$item4Total', '$item5Qty', '$item5Name', '$item5Rate', '$item5Total', '$item6Qty', '$item6Name', '$item6Rate', '$item6Total', '$item7Qty', '$item7Name', '$item7Rate', '$item7Total', '$item8Qty', '$item8Name', '$item8Rate', '$item8Total', '$item9Qty', '$item9Name', '$item9Rate', '$item9Total', '$item10Qty', '$item10Name', '$item10Rate', '$item10Total', '$subTotal', '$discount', '$tax', '$total', '$notes')";
		
		// Execute MySQL query, if error die displaying error message
		mysql_query($insert) or die(mysql_error());

		// Close the connection
		mysql_close();
	}

?>

<!-- After execution of MySQL query, redirect to invoices.php //-->
<html>
    <head>
        <meta http-equiv="refresh" content="0;url=invoices.php">
    </head>
</html>
